package interfaces.spawnMenu.costumize.items;

import java.util.List;

import networking.messages.syncMessages.ValueType;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.util.Color;

import fileHandling.language.Lang;
import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.SpawnMenuText;

import logic.common.player.playerListeners.PlayerValueListener;
import logic.ships.equipmentListener.EquipmentListener;
import logic.ships.equipmentListener.ItemChangedEvent;
import logic.ships.equipmentListener.WeaponChangedEvent;
import logic.ships.hunter.Hunter;
import logic.status.StatusChangedEvent;
import logic.weapons.Weapon;
import logic.weapons.WeaponType;
import logic.weapons.projectileWeapons.ProjectileChangedEvent;
import logic.weapons.projectileWeapons.ProjectileListener;
import logic.weapons.projectileWeapons.ProjectileWeapon;
import interfaces.GUISource;
import interfaces.superWidgets.InterfaceButton;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.ListenerKeeperElement;
import interfaces.superWidgets.StaticContent;

public class ProjectilesContent extends StaticContent {
	
	private final Color ENOUGH_MONEY_COLOR = Color.BLACK;
	private final Color NOT_ENOUGH_MONEY_COLOR = Color.RED;	
	
	protected InterfaceLabel titleLabel;
	protected InterfaceButton fillOneButton, fillAllButton;
	protected WeaponType weaponType;
	protected Hunter hunter;
	protected Lang weaponsLang;
	protected String weaponsText;
	protected int projectileCosts, currentProjectiles, maxProjectiles, money;
	protected EquipmentListener equipmenListener;
	protected ProjectileListener projectileListener;
	protected PlayerValueListener playerListener;
	protected ListenerKeeperElement element;
	
	public ProjectilesContent(int width, int height, int x, int y, WeaponType type, ListenerKeeperElement element) {
		super(width, height, x, y);
		this.element = element;
		
		weaponType = type;
		
		ImageFont font = GUISource.middleFont;
		
		weaponsLang = SpawnMenuText.HeatSeekers;
		projectileCosts = ProjectileWeapon.getProjectileCosts(WeaponType.HeatSeeker);
		if(weaponType == WeaponType.Missile) {
			weaponsLang = SpawnMenuText.Missiles;
			projectileCosts = ProjectileWeapon.getProjectileCosts(WeaponType.Missile);
		}
		
		int labelWidth = width / 2;
		int buttonWidth = (width - labelWidth) / 2;
		
		weaponsText = LanguageLoader.get(weaponsLang);
		
		titleLabel = new InterfaceLabel(weaponsText + " (0/0)", font);
		titleLabel.setXY(0, 0);
		titleLabel.setSize(labelWidth, height);
		addWidget(titleLabel);
		
		fillOneButton = new InterfaceButton("1 (" + projectileCosts + ")", font);
		fillOneButton.setSize(buttonWidth, height);
		fillOneButton.setXY(labelWidth, 0);
		fillOneButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				int projectiles = maxProjectiles - currentProjectiles;
				if(projectiles > 0) {
					hunter.restockProjectiles(weaponType, 1);
					hunter.getPilot().spendMoney(projectileCosts);
					updateTexts();
				}
			}
		});
		addWidget(fillOneButton);
		
		fillAllButton = new InterfaceButton("0", font);
		fillAllButton.setSize(buttonWidth, height);
		fillAllButton.setXY(labelWidth + buttonWidth, 0);
		fillAllButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				int projectiles = maxProjectiles - currentProjectiles;
				if(projectiles > 0) {
					hunter.restockProjectiles(weaponType, projectiles);
					hunter.getPilot().spendMoney(projectileCosts * projectiles);
					updateTexts();
				}
			}
		});
		addWidget(fillAllButton);
		
		layout();
		
		createListeners();
	}
	
	protected void createListeners() {
		projectileListener = new ProjectileListener() {
			@Override
			public void projectilesChanged(ProjectileChangedEvent event) {
				currentProjectiles -= event.getOldAmount();
				currentProjectiles += event.getNewAmount();
				updateTexts();
			}
		};
		
		equipmenListener = new EquipmentListener() {
			@Override
			public void weaponAdded(WeaponChangedEvent event) {
				addWeapon(event.getWeapon());
			}
			
			@Override
			public void weaponRemoved(WeaponChangedEvent event) {
				Weapon weapon = event.getWeapon();
				if(weapon.getWeaponType() == weaponType) {
					ProjectileWeapon pWeapon = (ProjectileWeapon)weapon;
					currentProjectiles -= pWeapon.getProjectilesLeft();
					maxProjectiles -= pWeapon.getMaxProjectiles();
					pWeapon.removeProjectileListener(projectileListener);
					updateTexts();
				}
			}
			
			@Override
			public void itemAdded(ItemChangedEvent event) {}
			@Override
			public void itemUsed(ItemChangedEvent event) {}
			@Override
			public void itemRemoved(ItemChangedEvent event) {}
		};
		
		playerListener = new PlayerValueListener() {
			@Override
			public void valueChanged(StatusChangedEvent event) {
				if(event.getType() == ValueType.Money) {
					money = event.getIntValue();
					updateTexts();
				}
			}
		};
	}
	
	private void addWeapon(Weapon weapon) {
		if(weapon.getWeaponType() == weaponType) {
			ProjectileWeapon pWeapon = (ProjectileWeapon)weapon;
			currentProjectiles += pWeapon.getProjectilesLeft();
			maxProjectiles += pWeapon.getMaxProjectiles();
			element.addListener(pWeapon, projectileListener);
			updateTexts();
		}
	}
	
	public void changeHunter(Hunter h) {
		if(hunter != null) {
			hunter.removeEquipmentListener(equipmenListener);
			hunter.getPilot().removePlayerValueListener(playerListener);
		}
		
		hunter = h;
		if(hunter != null) {
			for(List<Weapon> weapons : hunter.getAllWeapons().values()) {
				for(Weapon w : weapons) {
					addWeapon(w);
				}
			}
			element.addListener(hunter, equipmenListener);
			element.addListener(hunter.getPilot(), playerListener);
		}
	}
	
	protected void updateTexts() {
		titleLabel.setText(weaponsText + " (" + currentProjectiles + "/" + maxProjectiles + ")");
		int projectiles = maxProjectiles - currentProjectiles;
		int costs = projectiles * projectileCosts;
		fillAllButton.setText("" + projectiles + " (" + costs + ")");
		
		boolean enoughMoney = money >= projectileCosts;
		if(enoughMoney) GUISource.setTextColor(fillOneButton, ENOUGH_MONEY_COLOR);
		else GUISource.setTextColor(fillOneButton, NOT_ENOUGH_MONEY_COLOR);
		fillOneButton.setEnabled(enoughMoney);
		
		enoughMoney = money >= costs;
		if(enoughMoney) GUISource.setTextColor(fillAllButton, ENOUGH_MONEY_COLOR);
		else GUISource.setTextColor(fillAllButton, NOT_ENOUGH_MONEY_COLOR);
		fillAllButton.setEnabled(enoughMoney);
	}
}